package com.neusoft.hospital.controller;
/**
 * @author:杨浩杰
 * 测试类 CiReport
 *	option：六个原始操作（增、删、查<三个查找>、改）
 *	opton2：服务器接口测试方法
 */
import com.neusoft.hospital.pojo.CiReport;
import com.neusoft.hospital.service.CirService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*" ,allowCredentials = "true")
@RequestMapping("/cir")
public class CirController {

	@Autowired
	CirService cirService;


//===================================================select option========================================================

	/*	select cireport全部数据列表
	 * 	http://localhost:8080/cir/AllCirList
	 */
	@RequestMapping("/AllCirList")
	public Map<String, Object> AllCirList() throws Exception{
		List<CiReport> cirList = cirService.findCirBySql();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", "1");
		map.put("cirList", cirList);
		return map;
	}

	/*	select cireport 模糊查询
	 * 	http://localhost:8080/cir/getCirByName?ciName=哈
	 */
	@RequestMapping("/getCirByName")
	public Map<String, Object> getCirByName(String ciName) throws Exception{
		List<CiReport> cireportList = cirService.findCirByName(ciName);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", "1");
		map.put("cirList", cireportList);
		return map;
	}

	/*	select cireport by cirId
	 * 	http://localhost:8080/cir/getCirBycirId?cirId=1
	 */
	@RequestMapping("/getCirBycirId")
	public Map<String, Object> getCirBycirId(int cirId) throws Exception{
		List<CiReport> cireportList = cirService.findCirBycirid(cirId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", "1");
		map.put("cirList", cireportList);
		return map;
	}


//===================================================insert============================================================

	/*	数据库的增加操作
	 * 	http://localhost:8080/cir/InsetCir?cirId=11&ciId=11&ciName="wule"&orderId=12
	 */
	@RequestMapping("/InsetCir")
	public Map<String, Object> InsetCir(HttpSession session, CiReport cireport) throws Exception{
		int ciId = cireport.getCiId();
		CiReport target = cirService.findTaskByCiId(ciId);
		Map<String, Object> map = new HashMap<String, Object>();
		if(target == null){
			cirService.insertCir(cireport);
			map.put("status", "1");
			map.put("url", "/cir_ajax/toSuccess");
		} else {
			map.put("status", "0");
			map.put("infoCirInsert", " the account has already exit");
		}
		return map;
	}



//===================================================update============================================================
	/*	update
	 * 	http://localhost:8080/cir/doUpdate?cirId=11&ciId=11&ciName="wule"&orderId=12
	 */
	@RequestMapping("/doUpdate")
	public Map<String, Object> doUpdate(CiReport cireport, String enable) throws Exception{
		cirService.updateCir(cireport);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", "1");
		return map;
	}


//===================================================delete============================================================
	/*	delete
	 * 	http://localhost:8080/cir/delByCirId?cirId=11
	 */
	@RequestMapping("/delByCirId")
	public Map<String, Object> delByCirId(HttpSession session, CiReport cireport) throws Exception{
		int count = cirService.deleteCir(cireport);
		Map<String, Object> map = new HashMap<String, Object>();
		if(count>0){
			map.put("status", "1");
		} else {
			map.put("status", "0");
		}
		return map;
	}


//============================================服务器接口ciReport/listCiReport===========================================
	/*	关联查询
	 * 	http://localhost:8080/cir/ciReport
	 */
	@RequestMapping("/ciReport")
	public Map<String, Object> ciReport() throws Exception{
		List<CiReport> listCiReport = cirService.findcireport();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", "1");
		map.put("cirList", listCiReport);
		return map;
	}

}
